package com.haidechizi.util.lock.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @author: wangdejun
 * @create: 2019-12-10 20:53
 *
 * 初始化lock时间监听器
 **/
@Component
public class LockListenerInitializer implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private LockEventManager lockEventManager;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        if (applicationContext != null) {
            String[] beanNamesForType = applicationContext.getBeanNamesForType(LockListener.class);
            for (String beanName : beanNamesForType) {
                LockListener lockListener = (LockListener) applicationContext.getBean(beanName);
                lockEventManager.addListener(lockListener);
            }
        }
    }
}
